<?php

namespace app\common\model;

use think\Model;

/**
 * 资金流动
 * 字段：
 * cash_flow_status:资金流动状态，未确认，正在进行（产生效果），已完成（产生效果），失败
 * cash_type:流动的资金类型，财富币，报单币；
 * cash_flow_type:资金流动的类型，奖金，充值，消费，提现
 * confirm_time:生效日期
 * cash_value:资金流动数额
 * cash_flow_comments:资金流动的备注
 * 外部关系：
 * target_user:发生资金变动的账户
 * confirmed_user:对于要批准的流动，批准人 or null.
 */
class CashFlow extends Model
{
    protected $table = 'ycyp_cash_flow';
    protected $subattr = [];

    public function target_user()
    {
        return $this->belongsTo('User', 'user_id');
    }
    public function confirmed_user()
    {
        return $this->belongsTo('User', 'confirm_user', 'user_id');
    }
    public static $cash_flow_status_category = [0 => '未确认', 1 => '正在进行', 2 => '已完成', 3 => '失败'];
    public function getCashFlowStatusAttr($value)
    {
        return self::$cash_flow_status_category[$value];
    }
    public function setCashFlowStatusAttr($value, $data)
    {
        $catedata = array_flip(self::$cash_flow_status_category);
        $this->confirm_time = date('Y-m-d H:i:s');
        if (($value == '已完成' or $value == '正在进行') and ($data['cash_flow_status'] == 0 or $data['cash_flow_status'] == 3)) {
            if ($data['cash_type'] == 0) {
                $this->target_user->money_cash = $this->target_user->money_cash + $data['cash_value'];
            } else {
                $this->target_user->line_cash = $this->target_user->line_cash + $data['cash_value'];
            }
            $this->target_user->save();
        } elseif (($value == '未确认' or $value == '失败') and ($data['cash_flow_status'] == 1 or $data['cash_flow_status'] == 2)) {
            if ($data['cash_type'] == 0) {
                $this->target_user->money_cash = $this->target_user->money_cash - $data['cash_value'];
            } else {
                $this->target_user->line_cash = $this->target_user->line_cash - $data['cash_value'];
            }
            $this->target_user->save();
        }

        return $catedata[$value];
    }
    public function setCashValueAttr($value, $data)
    {
        if ($data['cash_flow_status'] == 1 or $data['cash_flow_status'] == 2) {
            if ($data['cash_type'] == 0) {
                $this->target_user->money_cash = $this->target_user->money_cash - $data['cash_value'];
            } else {
                $this->target_user->line_cash = $this->target_user->line_cash - $data['cash_value'];
            }
            if ($data['cash_type'] == 0) {
                $this->target_user->money_cash = $this->target_user->money_cash + $value;
            } else {
                $this->target_user->line_cash = $this->target_user->line_cash + $value;
            }
            $this->target_user->save();
        }

        return $value;
    }
    public static $cash_type_category = [0 => '财富币', 1 => '报单币'];
    public function getCashTypeAttr($value)
    {
        return self::$cash_type_category[$value];
    }
    public function setCashTypeAttr($value)
    {
        $data = array_flip(self::$cash_type_category);

        return $data[$value];
    }
    public static $cash_flow_type_category = [0 => '奖金', 1 => '充值', 2 => '消费', 3 => '提现'];
    public function getCashFlowTypeAttr($value)
    {
        return self::$cash_flow_type_category[$value];
    }
    public function setCashFlowTypeAttr($value)
    {
        $data = array_flip(self::$cash_flow_type_category);

        return $data[$value];
    }

    public function getAttr($name)
    {
        if (in_array($name, $this->subattr)) {
            if($this->sub)
            {
                return $this->sub->getAttr($name);
            }else{
              return 'null';
            }

        }

        return parent::getAttr($name);
    }
    public function setAttr($name, $value, $data = [])
    {
        if (in_array($name, $this->subattr)) {
            return $this->sub->setAttr($name, $value, $data);
        }

        return parent::setAttr($name, $value, $data);
    }
    public function sub()
    {
        switch ($this->cash_flow_type) {
        case '奖金':
          return $this->hasOne('AwardRelation', 'cash_flow_id');
          break;
        case '充值':
          return $this->hasOne('ChargeRelation', 'cash_flow_id');
          break;
        case '消费':
          return $this->hasOne('ConsumeRelation', 'cash_flow_id');
          break;
        case '提现':
          return $this->hasOne('EncashmentRelation', 'cash_flow_id');
          break;
      }
    }
    public function save($data = [], $where = [], $getId = true, $replace = false)
    {
        $subdata = [];
        foreach ($data as $key => $value) {
            if (in_array($key, $this->subattr)) {
                $subdata[$key] = $value;
                unset($data[$key]);
            }
        }
        $res = parent::save($data, $where, $getId, $replace);
        if(count($subdata)!=0)
        {
          if ($this->sub->isUpdate) {
              $this->sub->save($subdata);
          } else {
              $this->sub()->save($subdata);
          }
        }
        return $res;
    }
}
